<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends Front_Controller {

	public function __construct(){
        parent::__construct();
		$this->load->model(array("category_model","post_model",'provinder_model'));
		$this->config->load ( 'frontend' );
    }

	public function index($addr=""){
		$data = array();
		if(!empty($addr)){
			getAddrBySlugUrl ( $addr );
		}
		$listFeatured = $this->post_model->getFeatured($this->province_id,21);
        $data['category_id']=0;
        $data['listFeatured']=$listFeatured;
		$this->layout->view($data);
	}

    public function shop($username=""){
        $data=array();

        $data['category_id']=0;
        $this->layout->view($data);
    }

	function home_data($home_setting){
		$data = array ();
		foreach ( $home_setting as $key=>$cat ) {
			$data[$key] = $cat;
			$post_vip = $this->post_model->get_home_data_vip($cat['id'],6,1);
			$limit = 6-count($post_vip);
			$post=array();
			if($limit>0){
				$post = $this->post_model->get_home_data_post($cat['id'],$limit);
			}
			$data[$key]['item'] = array_merge($post_vip,$post);
		}
		return $data;
	}

	private function home_data_left($home_left_setting, $provinder_id) {
		$data = array ();
		$post =array();
		foreach ( $home_left_setting as $key=>$cat ) {
			$data[$key] = $cat;
			$post_vip = $this->post_model->get_post_vip($provinder_id,$cat['id'],6,1);
			$limit = 6-count($post_vip);
			if($limit>0){
				$post = $this->post_model->get_post_home($provinder_id,$cat['id'],$limit);
			}
			$data[$key]['item'] = array_merge($post_vip,$post);
		}
		//save cache in 1 minutes
		$this->cache->save ( 'home_data_left_'.$provinder_id, $data, 60 );
		return $data;
	}

	private function home_data_right($home_right_setting, $provinder_id) {
		$data = array ();
		$post =array();
		foreach ( $home_right_setting as $key=>$cat ) {
			$data[$key] = $cat;
			$post_vip = $this->post_model->get_post_vip($provinder_id,$cat['id'],6,1);
			$limit = 6-count($post_vip);
			if($limit>0){
				$post = $this->post_model->get_post_home($provinder_id,$cat['id'],$limit);
			}
			$data[$key]['item'] = array_merge($post_vip,$post);
		}
		//save cache in 1 minutes
		$this->cache->save ( 'home_data_right_'.$provinder_id, $data, 60 );
		return $data;
	}

	public function index1()
	{

		$this->load->library('pagination');
		$this->load->model('category_model');
		$config['base_url'] = base_url().'home/index';
		$config['total_rows'] =  $this->category_model->count_all();
		$config['per_page'] =  10;
		$config['suffix']= ".html";
		$config['use_page_numbers'] = TRUE;

		//$config['uri_segment'] = 3;
		$this->pagination->initialize($config);

        $data['records'] = $this->category_model->find_all($config['per_page'], $this->uri->segment(3));

		$this->layout->view('home/index',$data);
	}

	public function articles()
	{

		$this->load->library('pagination');
		$this->load->model('category_model');
		$config['base_url'] = base_url().'home/index';
		$config['total_rows'] =  $this->category_model->count_all();
		$config['per_page'] =  10;
		//$config['uri_segment'] = 3;
		$this->pagination->initialize($config);
		$this->db->order_by('orderNo', 'ASC');
        $data['records'] = $this->category_model->find_all($config['per_page'], $this->uri->segment(3));

		$this->layout->view('home/articles',$data);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
